#include "stdafx.h"
#include <windows.h>
#include "Shlwapi.h"
#include "Localises.h"
#include "Logging.h"

int TLocalises::LoadStrings(wchar_t *Path){
	HANDLE hFindFiles = INVALID_HANDLE_VALUE;
	try {
		int iPathDoesExist = PathFileExistsW(Path);

		if (iPathDoesExist == 1) {

			wchar_t sLanguageFolder[MAX_PATH];
			wcscpy_s(sLanguageFolder, MAX_PATH, Path);
			wcscat_s(sLanguageFolder, MAX_PATH, L"\\*.xml");

			WIN32_FIND_DATAW ffd;
			hFindFiles = FindFirstFileW(sLanguageFolder, &ffd);

			if (hFindFiles == INVALID_HANDLE_VALUE) {
				LogMessage("Error on Enumerating Language Files!", LOG_ERROR);
				return 0;
			}

			do {
				ProcessFile(ffd.cFileName, Path);
			} while (FindNextFileW(hFindFiles, &ffd) != 0);

		}
	}
	catch (std::exception e) {	
		//LogMessage("Error on load Language Files!", e.what(), LOG_ERROR);
	}

	if (hFindFiles != INVALID_HANDLE_VALUE) FindClose(hFindFiles);
	
}

LPWSTR TLocalises::GetText(const wchar_t *Key){
	//g_LanguageItems.at(Key);
	std::map<const std::wstring, const wchar_t *>::const_iterator it = g_LanguageItems.find(Key);
	if (it == g_LanguageItems.end())
		return (wchar_t *)Key;
	else
		return (wchar_t *)it->second;
		
}

const wchar_t *MbsToWcs(const char *input) {
	const size_t cSize = strlen(input) + 1;
	wchar_t* sreturn = new wchar_t[cSize];
	mbstowcs(sreturn, input, cSize);

	return sreturn;
}

void TLocalises::ProcessFile(wchar_t *File, wchar_t *Path){
	try {
		

		wchar_t xmlFile[MAX_PATH];
		wcscpy_s(xmlFile, MAX_PATH, Path);
		wcscat_s(xmlFile, MAX_PATH, L"\\");
		wcscat_s(xmlFile, MAX_PATH, File);
		//Load XML - Document
		if (PathFileExistsW(xmlFile) == 1){
			char dstxmlfile[MAX_PATH];
			wcstombs(dstxmlfile, xmlFile, MAX_PATH);

			pugi::xml_document doc;

			pugi::xml_parse_result result = doc.load_file(dstxmlfile);

			TiXmlDocument xmlDoc(dstxmlfile);

			bool bLoadResult = xmlDoc.LoadFile(dstxmlfile, TIXML_ENCODING_UTF8);

			char *sErrorMessage = "Unable to load language File '";
			char sErrorExceptionMessage[950];
			strcpy_s(sErrorExceptionMessage, 950, sErrorMessage);
			
			char sLanguageFile[MAX_PATH];

			wcstombs(sLanguageFile, File, MAX_PATH);
			strcat_s(sErrorExceptionMessage, 950, sLanguageFile);
			strcpy_s(sErrorExceptionMessage, 950, "'");

			if (!bLoadResult) throw std::exception(sErrorExceptionMessage);
			
			//tinyxml2::XMLNode* Node = 0;
			TiXmlNode* Node = 0;
			Node = xmlDoc.FirstChild("LanguageFile");

			//tinyxml2::XMLElement* ItemElement = 0;
			TiXmlElement* ItemElement = 0;
			ItemElement = xmlDoc.FirstChildElement("LanguageFile");
			//ItemElement = Node->ToElement();
			const char* sFileLanguage;
			TiXmlElement* MenuItemsElement = ItemElement->FirstChildElement("menuitems");
			//tinyxml2::XMLElement* MenuItemsElement = ItemElement->FirstChildElement(L"menuitems");
			//build a list from items to access over key
			for (ItemElement = MenuItemsElement->FirstChildElement(); ItemElement; ItemElement = ItemElement->NextSiblingElement()){
				const wchar_t *wcKey;
				const wchar_t *wcText;
				wcKey = MbsToWcs(ItemElement->Attribute("Key"));
				wcText = MbsToWcs(ItemElement->Attribute("Text"));
				//wcKey = ItemElement->Attribute(L"Key");// MbsToWcs(ItemElement->Attribute(L"Key"));
				//wcText = ItemElement->Attribute(L"Text");// MbsToWcs(ItemElement->Attribute("Text"));
				g_LanguageItems[wcKey] = wcText;
			}
			//MenuItemsHandle
		}
	}
	catch (std::exception e) {
		LogMessage("Error:", LOG_ERROR);
	}
}